package com.sinvo.oa.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sinvo.oa.log.annotation.Log;
import com.sinvo.oa.log.enums.ServiceType;
import com.sinvo.oa.service.SysUserService;
import com.sinvo.oa.exception.CustomException;
import com.sinvo.oa.result.R;
import com.sinvo.oa.result.ResultCode;
import com.sinvo.oa.entity.model.system.SysUser;
import com.sinvo.oa.entity.vo.system.SysUserQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @Description 用户控制器
 */
@Api(tags = "用户管理接口")
@RestController
@RequestMapping("admin/system/sysUser")
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;

    /**
     * 条件分页查询
     *
     * @param pageNum        当前页
     * @param pageSize       页大小
     * @param sysUserQueryVo 查询username以及时间范围
     * @return R
     */
    @PreAuthorize("hasAuthority('btn.sysUser.list')")
    @ApiOperation("条件分页查询")
    @GetMapping("{pageNum}/{pageSize}")
    public R pageQueryRole(@PathVariable Long pageNum,
                           @PathVariable Long pageSize,
                           SysUserQueryVo sysUserQueryVo) {

        //pageNum 当前页  pageSize 每页显示记录数
        Page<SysUser> page = new Page<>(pageNum, pageSize);
        page = sysUserService.findUserByPage(page,sysUserQueryVo);

        return R.success(page);
    }

    /**
     * 根据userid查询用户
     *
     * @param id userId
     * @return R
     */
    @PreAuthorize("hasAuthority('btn.sysUser.list')")
    @ApiOperation(value = "获取用户")
    @GetMapping("get/{id}")
    public R get(@PathVariable Long id) {
        //验证参数
        if (StringUtils.isEmpty(id)) {
            throw new CustomException(ResultCode.DATA_ERROR.getCode(), "非法参数");
        }
        SysUser user = sysUserService.getById(id);
        return R.success(user);
    }

    /**
     * 添加用户
     *
     * @param user 用户实体
     * @return R
     */
    @Log(title = "用户管理",businessType = ServiceType.INSERT)
    @PreAuthorize("hasAuthority('btn.sysUser.add')")
    @ApiOperation(value = "保存用户")
    @PostMapping("save")
    public R save(@Validated @RequestBody SysUser user, BindingResult result) {
        if (result.hasErrors()) {
            throw new CustomException(ResultCode.DATA_ERROR.getCode(), "非法参数");
        }
        sysUserService.saveUser(user);

        return R.success();
    }

    /**
     * 更新用户
     *
     * @param user 用户实体
     * @return R
     */
    @Log(title = "用户管理",businessType = ServiceType.UPDATE)
    @PreAuthorize("hasAuthority('btn.sysUser.update')")
    @ApiOperation(value = "更新用户")
    @PutMapping("update")
    public R updateById(@Validated @RequestBody SysUser user, BindingResult result) {
        if (result.hasErrors()) {
            throw new CustomException(ResultCode.DATA_ERROR.getCode(), "非法参数");
        }
        sysUserService.updateUser(user);
        return R.success();
    }

    /**
     * 删除用户
     *
     * @param id userid
     * @return R
     */
    @Log(title = "用户管理",businessType = ServiceType.DELETE)
    @PreAuthorize("hasAuthority('btn.sysUser.remove')")
    @ApiOperation(value = "删除用户")
    @DeleteMapping("remove/{id}")
    public R remove(@PathVariable Long id) {
        //验证参数
        if (StringUtils.isEmpty(id)) {
            throw new CustomException(ResultCode.DATA_ERROR.getCode(), "非法参数");
        }
        boolean success = sysUserService.removeUser(id);
        if (!success) {
            throw new CustomException(ResultCode.SERVICE_ERROR.getCode(), "删除失败，请重试");
        }
        return R.success();
    }

    /**
     * 改变用户状态
     *
     * @param id     userId
     * @param status 1/0
     * @return R
     */
    @Log(title = "用户管理",businessType = ServiceType.UPDATE)
    @PreAuthorize("hasAuthority('btn.sysUser.update')")
    @ApiOperation(value = "更新用户状态")
    @GetMapping("updateStatus/{id}/{status}")
    public R updateStatus(@PathVariable Long id, @PathVariable Integer status) {
        //验证参数 id为空或者status不等于1或0 提示错误
        if (StringUtils.isEmpty(id) || !(status == 1 || status == 0)) {
            throw new CustomException(ResultCode.DATA_ERROR.getCode(), "非法参数");
        }
        boolean success = sysUserService.changeStatus(id, status);
        if (!success) {
            throw new CustomException(ResultCode.SERVICE_ERROR.getCode(), "修改失败，请重试");
        }
        return R.success();
    }
}

